package coop.bancocredicoop.proyectos.gd.mail;

import java.util.*;

import javax.mail.*;
import javax.mail.internet.*;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class MailSender {

	private Properties properties;
	private static final Log log = LogFactory.getLog(MailSender.class);

	public MailSender(String host) {
		properties = System.getProperties();
	    properties.setProperty("mail.smtp.host", host);
	}
	
	public void send(String from, String to, String subject, String text) throws MessagingException {

      Session session = Session.getDefaultInstance(properties);

      try{
         MimeMessage message = new MimeMessage(session);
         message.setFrom(new InternetAddress(from));
         message.addRecipient(Message.RecipientType.TO,
                                  new InternetAddress(to));
         message.setSubject(subject);
         message.setText(text);
         Transport.send(message);
         log.info("Sent message successfully....");
      } catch (MessagingException e) {
    	 log.error("Error sending mail", e);
    	 throw e;
      }
      
	}
	
}
